define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bios/storage/index' + location.search,
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    import_url: '',
                    dragsort_url: '',
                    table: 'bios_storage',
                    batch_url: 'storepro/batchlist' + location.search,
                    pinxi_url: 'storepro/pinxilist' + location.search,
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id desc, createtime DESC',
                pageSize: 500,                  // @DEMO
                pageList: [500],
                fixedColumns: true,
                fixedRightNumber: 1,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true ,operate: false },
                        {field: 'factory_id', title: __('Factory_id'), sortable: true},
                        {field: 'storea_id', title: __('Storea_id'), sortable: true},
                        {field: 'num', title: __('数量'), sortable: true, operate: false },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate ,
                            buttons: [
                                {name: 'child1', text: '批次', extend:'data-toggle="tooltip"', title: '查询', icon: '', classname: 'btn btn-xs btn-info btn-dialog',
                                    url: Controller.api.formatter.viewlist
                                },
                                {name: 'child2', text: '品系', extend:'data-toggle="tooltip"', title: '查询', icon: '', classname: 'btn btn-xs btn-info btn-dialog',
                                    url: Controller.api.formatter.pinxilist
                                }
                            ]
                        },

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'bios/storage/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'bios/storage/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'bios/storage/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                viewlist: function (value, row, index) {
                    var url = "/wMOsQTWmqR.php/storepro/batchlist?storea_id=" + value.storea + "&factory_id=" + value.factory ;
                    //var url = "/wMOsQTWmqR.php/storepro/batchlist/storea_id/" + value.storea + "/factory_id/" + value.factory ;
                    return url;
                },
                pinxilist: function (value, row, index) {
                    var url = "/wMOsQTWmqR.php/storepro/pinxilist?storea_id=" + value.storea + "&factory_id=" + value.factory ;
                    //var url = "/wMOsQTWmqR.php/storepro/batchlist/storea_id/" + value.storea + "/factory_id/" + value.factory ;
                    return url;
                },

            }

        }
    };
    return Controller;
});
